package com.android.monee.binding.binder;

import com.android.monee.binding.Field;
import com.android.monee.binding.observable.Observable;

/**
 *  new GenericOneWayBinder3<NewBudget2, NewBudget2, NewBudget2.Name, BindableEditText, BindableEditText.Text, String>(
 *				newBudget, newBudget.new Name(), budgetNameET, budgetNameET.new Text());
 *
 *  new GenericOneWayBinder3<BindableEditText, TextObservableAdapter, BindableEditText.Text, NewBudget2, NewBudget2.Name, String>(
 *				new TextObservableAdapter(budgetNameET), budgetNameET.new Text(), newBudget, newBudget.new Name());
 *
 */
public class Simple2WBinder<S,
                            So extends Observable<S>,
                            Sf extends Field<S, V>,
                            T,
                            To extends Observable<T>,
                            Tf extends Field<T, V>,
                            V> {
	
	private Simple1WBinder<S, So, Sf, T, Tf, V> srcToTrgt;
	private Simple1WBinder<T, To, Tf, S, Sf, V> trgtToSrc;
	
	public Simple2WBinder(So sourceObservable, Sf sourceField, To targetObservable, Tf targetField){
		final Simple2WBinder<S, So, Sf, T, To, Tf, V> that = this;
		
		this.srcToTrgt = new Simple1WBinder<S, So, Sf, T, Tf, V>
			(sourceObservable, sourceField, targetObservable.getObservedData(), targetField, false){
				@Override
				public void updateTarget(T target, V value) {
					that.updateTarget(target, value);
				}
		};
		
		this.trgtToSrc = new Simple1WBinder<T, To, Tf, S, Sf, V>
			(targetObservable, targetField, sourceObservable.getObservedData(), sourceField, false){
				@Override
				public void updateTarget(S target, V value) {
					that.updateSource(target, value);
				}
		};
		
		sourceObservable.notify(sourceField.getClass());
		targetObservable.notify(targetField.getClass());
	}
	
	public void updateTarget(T target, V value){
		this.srcToTrgt.defaultUpdateTraget(target, value);
	}
	
	public void updateSource(S target, V value){
		this.trgtToSrc.defaultUpdateTraget(target, value);
	}
}
